<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title><spring:message code='Publish.list'/></title>
	<meta name="decorator" content="default"/>
    <%@include file="/WEB-INF/views/include/treetable.jsp" %>
	<script type="text/javascript">
        $(document).ready(function() {
            var tpl = $("#treeTableTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
            var data = ${fns:toJson(list)}, rootId = "${not empty office.id ? office.id : '0'}";
            addRow("#treeTableList", tpl, data, rootId, true);
            $("#treeTable").treeTable({expandLevel : 5});
        });
        function addRow(list, tpl, data, pid, root){
            for (var i=0; i<data.length; i++){
                var row = data[i];
                if ((${fns:jsGetVal('row.parentId')}) == pid){
                    $(list).append(Mustache.render(tpl, {
                        dict: {
                            type: getDictLabel(${fns:toJson(fns:getDictList('sys_office_type'))}, row.type)
                        }, pid: (root?0:pid), row: row
                    }));
                    addRow(list, tpl, data, row.id);
                }
            }
        }
        function checkBox(obj){
            var isCheck = $(obj).is(":checked");
            var pTr = $(obj).parent().parent();
            var id = pTr.attr("id");
            var pId = pTr.attr("pId");
            var haschild = pTr.attr("haschild");

            if (isCheck) {
                checkChildren(id)
            } else {
                uncheckChildren(id)
            }
        }

        function checkChildren(pId){
            $("tr[pId='"+pId+"']").each(function(){
                if (this) {
                    $(this).find("input").prop("checked", true);
                    var id = $(this).attr("id");
                    checkChildren(id);
                } else {
                    return false;
                }
            });
        }

        function uncheckChildren(pId) {
            $("tr[pId='" + pId + "']").each(function () {
                if (this) {
                    $(this).find("input").prop("checked", false);
                    var id = $(this).attr("id");
                    uncheckChildren(id);
                } else {
                    return false;
                }
            });
        }

        function publish() {
            var egId = $("#egId").val();
            var equipmentLists = "";
            $("#treeTableList").find("input[type='checkbox']").each(function () {

                if ($(this).prop('checked')) {
                    var emId = $(this).attr("id");
                    var title = $(this).attr("title");
                    if (title == null || title == "") {
                        equipmentLists = equipmentLists + emId + ",";
                    }
                }
            });
            console.log("需要发布的设备编号:" + equipmentLists);
            if (equipmentLists == null || equipmentLists == "") {
                showTip("请选择目标设备!");
                return;
            }
            if (egId == null || egId == "") {
                showTip("请选择界面包!");
                return;
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/source/eleSourceGroup/publish",
                data: {equipmentLists: equipmentLists, egId: egId},
                datatype: "json",
                success: function (data) {
                    alertx(data.msg);
                    var sourcetype = $("#resourcetype").val();
                    if(sourcetype == "multimedia"){
                    	window.location.href="${ctx}/source/eleSourceMultimedia/?repage";
                    }else if(sourcetype == "title"){
                    	window.location.href="${ctx}/source/title/eleSourceTitle/?repage";
                    }else if(sourcetype == "subtitle"){
                    	window.location.href="${ctx}/source/subtitle/eleSourceSubtitleGroup/?repage";
                    }else if(sourcetype == "program"){
                    	window.location.href="${ctx}/source/program/eleSourceProgram?repage";
                    }else{
                    	window.location.href="${ctx}/source/eleSourceGroup/?repage";
                    }

                },
                error: function () {
                    //请求出错处理
                }
            });
        }
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active">
            <a href="${ctx}/source/eleTemplet/applypublish?id=${eleTemplet.id}">
                <spring:message code='Publish.list'/>
            </a>
        </li>
	</ul>
	<form:form id="searchForm" modelAttribute="eleEquipment" action="${ctx}/source/eleTemplet/applypublish" method="post" class="breadcrumb form-search">
		<input type="hidden" value="${eleSourceGroup.id}" name="egId" id="egId" />
		<input type="hidden" value="${resourcetype}" name="resourcetype" id="resourcetype" />
		
		<ul class="ul-form">
			<li><label><spring:message code="device.type"/>：</label>
				<sys:treeselect id="type" name="type.id" value="${eleEquipment.type.id}"
								labelName="type.name" labelValue="${eleEquipment.type.name}"
								title="<spring:message code='device.type'/>" url="/equipment/eleEquipmentType/treeData" cssClass="required input-small"/>
			</li>
            <shiro:hasRole name="admin">
                <li>
                    <label><spring:message code='common.company'/>：</label>
                    <sys:treeselect id="company" name="company.id" value="${eleEquipment.company.id}" labelName="company.name"
                                    labelValue="${eleEquipment.company.name}"
                                    title="<spring:message code='common.company'/>" url="/sys/office/treeData?type=1" cssClass="input-small" allowClear="true"/>
                </li>
			</shiro:hasRole>
            <li>
                <label><spring:message code='common.group'/>：</label>
                <shiro:lacksRole name="admin">
                    <input id="companyId" name="company.id" value="${eleEquipment.company.id}" type="hidden">
                </shiro:lacksRole>
                <sys:treeselect id="office" name="office.id" value="${eleEquipment.office.id}" labelName="office.name"
                                labelValue="${eleEquipment.office.name}"
                                title="<spring:message code='common.group'/>" url="/sys/office/treeData?type=2" cssClass="input-small" allowClear="true" notAllowSelectParent="true"/>
            </li>
            <li>
                <label style="width: 0;"></label>
              <spring:message code="common.device.id" var="deviceId"/>
                <form:input path="code" htmlEscape="false" maxlength="50" class="input-medium" placeholder="${deviceId}"/>            </li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="<spring:message code='common.Query'/>"/></li>
			<li class="clearfix"></li>
		</ul>
		
		
	</form:form>
	<sys:message content="${message}"/>
    <table id="treeTable" class="table table-striped table-bordered table-condensed">
        <thead><tr><th></th><th><spring:message code='common.organization'/></th><th><spring:message code="common.device.id"/></th><th><spring:message code='device.name'/><th><spring:message code='device.type'/></th><th><spring:message code='common.resolution'/></th><th><spring:message code='common.remarks'/></th>
        <tbody id="treeTableList"></tbody>
    </table>
    <div class="form-actions">

            <shiro:hasPermission name="source:eleSourceGroup:check">
                <a id="btnPublish" class="btn btn-primary" onclick="publish();">
                    <spring:message code="publish"/>
                </a>
            </shiro:hasPermission>
            <shiro:lacksPermission name="source:eleSourceGroup:check">
                <a id="btnPublish" class="btn btn-primary" onclick="publish();">
                    <spring:message code="publish"/>
                </a>
            </shiro:lacksPermission>

    </div>
    <script type="text/template" id="treeTableTpl">//<!--
        <tr id="{{row.id}}" pId="{{pid}}">
			<td>
				<input type="checkbox" id="{{row.id}}" title="{{row.name}}" onclick="checkBox(this)"/>
			</td>
            <td>{{row.name}}</td>
            <td>{{row.address}}</td>
            <td>{{row.zipCode}}</td>

            <td>{{row.email}}</td>
            <td>{{row.fax}}</td>
            <td>{{row.remarks}}</td>
        </tr>//-->
    </script>
</body>
</html>